home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / util / misc / VMM_src.lha / VMM / tools / ConvVMMCfg.c next >
Encoding:
C/C++ Source or Header  |  1995-12-16  |  12.1 KB  |  418 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <clib/dos_protos.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #ifdef __GNUC__
  8. #include "../shared_defs.h"
  9. #else
  10. #include "/shared_defs.h"
  11. #endif
  12.  
  13. static char rcsid [] = "$Id: ConvVMMCfg.c,v 3.3 95/12/16 19:05:04 Martin_Apel Rel $";
  14.  
  15. #define isspace(c) (((c)==' ') || ((c) == '\t') || ((c) == '\n'))
  16.  
  17. #define AT_LEAST_V1_1 ((Version > 1) || ((Version == 1) && (Revision >= 1)))
  18. #define AT_LEAST_V1_2 ((Version > 1) || ((Version == 1) && (Revision >= 2)))
  19. #define AT_LEAST_V1_3 ((Version > 1) || ((Version == 1) && (Revision >= 3)))
  20. #define AT_LEAST_V2_0 (Version >= 2)
  21. #define AT_LEAST_V2_1 ((Version > 2) || ((Version == 2) && (Revision >= 1)))
  22.  
  23. /* For version >= 1.1 the first two lines of the config file
  24.  * show the version and revision
  25.  * VERSION v
  26.  * 'v' is the version number
  27.  * REVISION r
  28.  *
  29.  * The next line of the config file is line stating the default value,
  30.  * whether to use virtual memory by default: (<= V1.2)
  31.  * DEFAULT     USE|NO_USE
  32.  * From V1.3 the line contains the minimum values for PUBLIC and
  33.  * non-PUBLIC allocations:
  34.  * DEFAULT     <n1> <n2>
  35.  *
  36.  * Next comes a line to tell how much memory is to be used for paging
  37.  * For versions <= V2.1 it looks like this:
  38.  * MEM         <n>|DYNAMIC
  39.  *
  40.  * Next comes a line to determine whether the statistics task is to
  41.  * to be started or not (<= V1.2)
  42.  * STAT        USE|NO_USE
  43.  * From V1.3 the line looks like this:
  44.  * STAT        USE|NO_USE <z> <zl> <zt> <ul> <ut>
  45.  * <z> is the zoomed state, <zl>... are the left and top edges for the
  46.  * zoomed and unzoomed state.
  47.  *
  48.  * One line for the partition which should be used for paging INCLUDING
  49.  * the terminating colon e.g. DH2:
  50.  * PART        <name>
  51.  * Instead of the last line the following two lines are possible
  52.  * FILE        <name>
  53.  * FILESIZE    <bytes>
  54.  * From V2.0 the keyword FILE may be replaced by PSEUDOPART to indicate
  55.  * usage of a pseudo-partition
  56.  *
  57.  * One line for the priority of the new memlist for virtual memory
  58.  * MEM_PRI     <n>
  59.  *
  60.  * From V1.1 upward here follows a line describing the flags to
  61.  * use for allocation of page frames
  62.  * MEM_FLAGS   <n>
  63.  *
  64.  * From V1.1 upward here follows a line stating whether to cache
  65.  * Zorro II RAM or not.
  66.  * Z2NO_CACHE  USE|NO_USE
  67.  *
  68.  * From V2.0 upward there follow four additional lines:
  69.  * For early beta versions of 2.0 the first entry is missing:
  70.  *
  71.  * WBPATCH     USE|NO_USE
  72.  * MIN_VM_ALLOC <n>
  73.  * ENABLE_HOTKEY <string>
  74.  * DISABLE_HOTKEY <string>
  75.  *
  76.  * For V2.1 there follows the size of the write buffer in bytes
  77.  * WRITE_BUFFER <n>
  78.  *
  79.  * Then there is an unknown number of lines which state for certain
  80.  * tasks if they should be granted virtual memory (<= V1.2)
  81.  * TASK   "task_name"    USE|NO_USE
  82.  * From V1.3 the line contains the minimum values for PUBLIC and
  83.  * non-PUBLIC allocations:
  84.  * TASK   "task_name"    <n1> <n2>
  85.  *
  86.  * That's it, for now
  87.  */
  88.  
  89. #define CFG_FIRST_LINE_V1_0  \
  90.     "DON'T EDIT !!! THIS FILE IS AUTOMATICALLY GENERATED.\n"
  91.  
  92. #define CFG_FIRST_LINE_FROM_1_1 \
  93.     "VMM40 CONFIGURATION. DON'T EDIT !!! FILE GENERATED AUTOMATICALLY.\n"
  94.  
  95. char *UseString = "USE";
  96. char *LargestString = "LARGEST";
  97. char *DynamicString = "DYNAMIC";
  98.  
  99. struct VMMConfig NewConfig =
  100.   {
  101.   CFG_MAGIC,                            /* CfgMagic              */
  102.   3,                                    /* Version               */
  103.   0,                                    /* Revision              */
  104.   DYN_MIN,                              /* MinMem                */
  105.   DYN_MAX,                              /* MaxMem                */
  106.   100 * 1024,                           /* WriteBuffer           */
  107.   40,                                   /* VMPriority            */
  108.   MEMF_FAST | MEMF_PUBLIC,              /* MemFlags              */
  109.   1,                                    /* FileSize              */
  110.   DEFAULT_MINVMSIZE,                    /* MinVMAlloc            */
  111.   0,                                    /* ZLeftEdge             */
  112.   0,                                    /* ZTopEdge              */
  113.   0,                                    /* UnZLeftEdge           */
  114.   0,                                    /* UnZTopEdge            */
  115.   USE_NEVER,                            /* DefaultMinPublic      */
  116.   USE_ALWAYS,                           /* DefaultMinNonPublic   */
  117.   0,                                    /* NumTaskEntries        */
  118.   PD_FILE,                              /* PageDev               */
  119.   DEFAULT_ENABLE_KEY,                   /* EnableHotkey          */
  120.   DEFAULT_DISABLE_KEY,                  /* DisableHotkey         */
  121.   "SYS:page_file",                      /* PartOrFileName        */
  122.   TRUE,                                 /* StatEnabled           */
  123.   FALSE,                                /* StatZoomed            */
  124.   FALSE,                                /* MemTracking           */
  125.   FALSE,                                /* PatchWB               */
  126.   TRUE,                                 /* CacheZ2RAM            */
  127.   CNOSWAP,                              /* DefaultCodePaging     */
  128.   DP_TRUE                               /* DefaultDataPaging     */
  129.   };
  130.   
  131.  
  132. /************************************************************************/
  133.  
  134. #define isspace(c) (((c)==' ') || ((c) == '\t') || ((c) == '\n'))
  135.  
  136. void GetNthString (char *from, char *to, int n)
  137.  
  138. {
  139. /* Extracts the nth string in the line "from" and copies it to "to".
  140.  * If the nth string does not exist 'to' is set to an empty string.
  141.  */
  142. char *tmp1, *tmp2;
  143.  
  144. tmp1 = from;
  145. tmp2 = to;
  146. while (*tmp1 != 0 && isspace (*tmp1)) tmp1++;    /* skip leading whitespace */
  147.  
  148. while (--n > 0)
  149.   {
  150.   while (*tmp1 != 0 && !isspace (*tmp1)) tmp1++; /* skip word */
  151.   while (*tmp1 != 0 &&  isspace (*tmp1)) tmp1++; /* skip blanks */
  152.   }
  153.  
  154. while (*tmp1 != 0 && !isspace(*tmp1)) *tmp2++ = *tmp1++;
  155. *tmp2 = 0;
  156. }
  157.  
  158. /***********************************************************************/
  159.  
  160. static UWORD val_to_dp_type (ULONG MinPublic, ULONG MinNonPublic)
  161.  
  162. /* Converts MinPublic and MinNonPublic values into the type of data paging
  163.  * used.
  164.  */
  165. {
  166. if (MinPublic != USE_NEVER)
  167.   return (DP_ADVANCED);
  168. else if (MinNonPublic == USE_NEVER)
  169.   return (DP_FALSE);
  170. else if (MinNonPublic == USE_ALWAYS)
  171.   return (DP_TRUE);
  172. else
  173.   return (DP_ADVANCED);
  174. }
  175.  
  176. /************************************************************************/
  177.  
  178. BOOL Convert (char *filename)
  179.  
  180. {
  181. char buffer [200],
  182.      line [200];
  183. char converted_filename [200];
  184. char *tmp;
  185. ULONG Version,
  186.       Revision;
  187. ULONG NumEntries = 0;
  188. struct OldTaskEntryInFile te;
  189.  
  190. BPTR old_cfg_file,
  191.      new_cfg_file;
  192.  
  193. if ((old_cfg_file = Open (filename, MODE_OLDFILE)) == NULL)
  194.   return (FALSE);
  195.  
  196. strcpy (converted_filename, filename);
  197. strcat (converted_filename, ".new");
  198.  
  199. line [0] = 0;
  200. FGets (old_cfg_file, line, 200 - 1);
  201. if (strcmp (line, CFG_FIRST_LINE_V1_0) == 0)
  202.   {
  203.   Version = 1;
  204.   Revision = 0;
  205.   }
  206. else if (strcmp (line, CFG_FIRST_LINE_FROM_1_1) == 0)
  207.   {
  208.   FGets (old_cfg_file, line, 200 - 1);
  209.   GetNthString (line, buffer, 2);
  210.   StrToLong (buffer, (LONG*)&Version);
  211.   FGets (old_cfg_file, line, 200 - 1);
  212.   GetNthString (line, buffer, 2);
  213.   StrToLong (buffer, (LONG*)&Revision);
  214.   }
  215. else
  216.   {
  217.   Close (old_cfg_file);
  218.   return (FALSE);
  219.   }
  220.  
  221. if ((new_cfg_file = Open (converted_filename, MODE_NEWFILE)) == NULL)
  222.   {
  223.   Close (old_cfg_file);
  224.   return (FALSE);
  225.   }
  226.  
  227. FGets (old_cfg_file, line, 200 - 1);               /* "DEFAULT" line */
  228. GetNthString (line, buffer, 2);
  229.  
  230. if (AT_LEAST_V1_3)
  231.   {
  232.   StrToLong (buffer, (LONG*)&(NewConfig.DefaultMinPublic));
  233.   GetNthString (line, buffer, 3);
  234.   StrToLong (buffer, (LONG*)&(NewConfig.DefaultMinNonPublic));
  235.   NewConfig.DefaultDataPaging = val_to_dp_type (NewConfig.DefaultMinPublic,
  236.                                    NewConfig.DefaultMinNonPublic);
  237.   }
  238.  
  239. FGets (old_cfg_file, line, 200 - 1);
  240. GetNthString (line, buffer, 2);          /* "MEM" line */
  241. if ((strcmp (buffer, LargestString) == 0) ||
  242.     (strcmp (buffer, DynamicString) == 0))
  243.   {
  244.   NewConfig.MinMem = DYN_MIN;
  245.   NewConfig.MaxMem = DYN_MAX;
  246.   }
  247. else
  248.   {
  249.   StrToLong (buffer,  (LONG*)&(NewConfig.MinMem));
  250.   NewConfig.MaxMem = NewConfig.MinMem;
  251.   }
  252.  
  253. FGets (old_cfg_file, line, 200 - 1);
  254. GetNthString (line, buffer, 2);
  255. NewConfig.StatEnabled = (strcmp (buffer, UseString) == 0);
  256.  
  257. if (AT_LEAST_V1_3)
  258.   {
  259.   GetNthString (line, buffer, 3);
  260.   NewConfig.StatZoomed = (*buffer == '1');
  261.   GetNthString (line, buffer, 4);
  262.   StrToLong (buffer, (LONG*)&(NewConfig.ZLeftEdge));
  263.   GetNthString (line, buffer, 5);
  264.   StrToLong (buffer, (LONG*)&(NewConfig.ZTopEdge));
  265.   GetNthString (line, buffer, 6);
  266.   StrToLong (buffer, (LONG*)&(NewConfig.UnZLeftEdge));
  267.   GetNthString (line, buffer, 7);
  268.   StrToLong (buffer, (LONG*)&(NewConfig.UnZTopEdge));
  269.   }
  270.  
  271. FGets (old_cfg_file, line, 200 - 1);
  272. if (strncmp (line, "PART", 4) == 0)
  273.   {
  274.   NewConfig.PageDev = PD_PART;
  275.   GetNthString (line, NewConfig.PartOrFileName, 2);
  276.  
  277.   if ((tmp = strchr (NewConfig.PartOrFileName, ':')) == NULL)
  278.     strcat (NewConfig.PartOrFileName, ":");
  279.   }
  280. else
  281.   {
  282.   /* It's paging to a file */
  283.   if (strncmp (line, "FILE", 4) == 0)
  284.     NewConfig.PageDev = PD_FILE;
  285.   else
  286.     {
  287.     NewConfig.PageDev = PD_PSEUDOPART;
  288.     }
  289.  
  290.   GetNthString (line, NewConfig.PartOrFileName, 2);
  291.  
  292.   /* Next line contains the size */
  293.   FGets (old_cfg_file, line, 200 - 1);
  294.   GetNthString (line, buffer, 2);
  295.   StrToLong (buffer, (LONG*)&(NewConfig.FileSize));
  296.   NewConfig.FileSize /= 1024 * 1024;         /* in MByte */
  297.   }
  298.  
  299. FGets (old_cfg_file, line, 200 - 1);
  300. GetNthString (line, buffer, 2);
  301. StrToLong (buffer, (LONG*)&(NewConfig.VMPriority));
  302.  
  303. if (AT_LEAST_V1_1)
  304.   {
  305.   FGets (old_cfg_file, line, 200 - 1);
  306.   GetNthString (line, buffer, 2);
  307.   StrToLong (buffer, (LONG*)&(NewConfig.MemFlags));
  308.  
  309.   FGets (old_cfg_file, line, 200 - 1);
  310.   GetNthString (line, buffer, 2);
  311.   NewConfig.CacheZ2RAM = (strcmp (buffer, UseString) != 0);
  312.   }
  313.  
  314. if (AT_LEAST_V2_0)
  315.   {
  316.   FGets (old_cfg_file, line, 200 - 1);
  317.   if (strncmp (line, "WBPATCH", 7) == 0)
  318.     {
  319.     GetNthString (line, buffer, 2);
  320.     NewConfig.PatchWB = (strcmp (buffer, UseString) == 0);
  321.     FGets (old_cfg_file, line, 200 - 1);
  322.     }
  323.  
  324.   GetNthString (line, buffer, 2);
  325.   StrToLong (buffer, (LONG*)&(NewConfig.MinVMAlloc));
  326.  
  327.   FGets (old_cfg_file, line, 200 - 1);
  328.   tmp = line;
  329.   while (*tmp != 0 && !isspace (*tmp)) tmp++;
  330.   while (*tmp != 0 && isspace (*tmp)) tmp++;
  331.   strcpy (NewConfig.EnableHotkey, tmp);
  332.   if ((tmp = strchr (NewConfig.EnableHotkey, '\n')) != NULL)
  333.     *tmp = 0;
  334.  
  335.   FGets (old_cfg_file, line, 200 - 1);
  336.   tmp = line;
  337.   while (*tmp != 0 && !isspace (*tmp)) tmp++;
  338.   while (*tmp != 0 && isspace (*tmp)) tmp++;
  339.   strcpy (NewConfig.DisableHotkey, tmp);
  340.   if ((tmp = strchr (NewConfig.DisableHotkey, '\n')) != NULL)
  341.     *tmp = 0;
  342.   }
  343.  
  344. if (AT_LEAST_V2_1)
  345.   {
  346.   FGets (old_cfg_file, line, 200 - 1);
  347.   GetNthString (line, buffer, 2);
  348.   StrToLong (buffer, (LONG*)&(NewConfig.WriteBuffer));
  349.   }
  350.  
  351. Write (new_cfg_file, &NewConfig, sizeof (struct VMMConfig));
  352.  
  353. te.CodePaging = FALSE;
  354. te.IsDefault = FALSE;
  355.  
  356. while (FGets (old_cfg_file, line, 200 - 1) != NULL)
  357.   {
  358.   char *task_name;
  359.  
  360.   tmp = &(line [0]);
  361.   while (*tmp != '"') tmp++;
  362.   task_name = ++tmp;
  363.   while (*tmp != '"') tmp++;
  364.   *tmp++ = 0;
  365.   strncpy (te.TaskName, task_name, 39);
  366.  
  367.   while (isspace(*tmp)) tmp++;
  368.   if (AT_LEAST_V1_3)
  369.     {
  370.     GetNthString (tmp, buffer, 1);
  371.     StrToLong (buffer, (LONG*)&(te.MinPublic));
  372.     GetNthString (tmp, buffer, 2);
  373.     StrToLong (buffer, (LONG*)&(te.MinNonPublic));
  374.     }
  375.   else
  376.     {
  377.     te.MinPublic = USE_NEVER;
  378.     if (strncmp (tmp, UseString, strlen (UseString)) == 0)
  379.       te.MinNonPublic = USE_ALWAYS;
  380.     else
  381.       te.MinNonPublic = USE_NEVER;
  382.     }
  383.   te.DataPaging = val_to_dp_type (te.MinPublic, te.MinNonPublic);
  384.  
  385.   NumEntries++;
  386.  
  387.   Write (new_cfg_file, &te, sizeof (struct OldTaskEntryInFile));  
  388.   }
  389.  
  390. #define OFFSET(x) ((ULONG)&(((struct VMMConfig*)NULL)->x))
  391.  
  392. Seek (new_cfg_file, OFFSET(NumTaskEntries), OFFSET_BEGINNING);
  393.  
  394. Write (new_cfg_file, &NumEntries, sizeof (ULONG));
  395.  
  396. Close (old_cfg_file);
  397. Close (new_cfg_file);
  398.  
  399. DeleteFile (filename);
  400. Rename (converted_filename, filename);
  401.  
  402. return (TRUE);
  403. }
  404.  
  405. /***********************************************************************/
  406.  
  407. void main (void)
  408.  
  409. {
  410. if (!Convert ("ENVARC:" CFG_FILEBASE))
  411.   printf ("Couldn't convert " CFG_FILEBASE " in ENVARC:\n");
  412.  
  413. if (!Convert ("ENV:" CFG_FILEBASE))
  414.   printf ("Couldn't convert " CFG_FILEBASE " in ENV:\n");
  415.  
  416. printf (PROGNAME " configuration file converted successfully\n");
  417. }
  418.